
package backupUtility;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.swing.JOptionPane;
/**
 * This class contains the logic behind the BackupUtilityUI.java
 * it is responsible for how the backup files are created, stored
 * and locked into a folder. This class implements the ZipBackup.java
 * which in turn, creates an archive file for the backup folder
 * @author Changcoco Kevin Paolo
 * @author Mamis Dominique Bernard
 * @author Palasi Jandy
 * @author Rafael Ian Paolo 
 */


public class BackupLogic {
   /**
    * Creates a new directory.
    * Copies the file to the directory.
    * Includes the File name and the password.
    * Includes file Zip.
    * Compresses file after files was relocated to directory.
    * Asks whether to add in existing file or create new directory.
    * Gets the files from the array FileGetter.
    *
    * @throws IOException	Throws exception when file already exist
    * @param ArrayList<File>	Array of files
    * @param String folder	Directory folder
    * @param String password	Input Password
    */
    public BackupLogic(ArrayList<File> selected, String folder, String password) throws IOException{
    
    try{    
    String fName = "D:/BACKUP/"+folder+"/";
    File directory = new File(fName);
     if (!directory.exists()) {
           JOptionPane.showMessageDialog(null,"Creating directory: " +directory.getName());
           boolean result = directory.mkdirs();  
            if(result) {    
              JOptionPane.showMessageDialog(null,"Backup created");  
            }
            
    ZipBackup zip = new ZipBackup(selected,fName, fName+""+folder+".zip", password);
    new BackupUtilityUI().setVisible(true);
      }else{		
           JOptionPane.showMessageDialog(null, "Backup folder already exists. Please select the Edit Backup option in the Main menu :) ");		
            new BackupUtilityUI().setVisible(true);
     }
    }catch(Exception e){}
    }

    
}
